home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Design Notes.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  10.4 KB  |  418 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS  *****************
  4.  
  5. var helpDoc = MM.HELP_cmdDesignNotes;
  6.  
  7. var KEY_STATUS = "status";
  8. var KEY_NOTES  = "notes";
  9. var KEY_OPEN   = "showOnOpen";
  10. var PAIR_SEP   = " = ";
  11. var DEFAULT_STATUS = 0;
  12.  
  13. var T = ''; //TabControl object
  14. var DATA;
  15. var FILE_PTR = 0;
  16.  
  17. var FILE_IS_WRITEABLE;
  18.  
  19. //******************* API **********************
  20.  
  21. function commandButtons(){
  22.   var btns;
  23.  
  24.   //if launched but not writable, don't show OK button
  25.   if (MMNotes.FileInfo_isWriteable != null && MMNotes.FileInfo_isWriteable == false) {
  26.     btns = new Array(BTN_Cancel, "cancelClicked()", BTN_Help, "displayHelp()");
  27.   } else {
  28.     btns = new Array(BTN_OK,     "okClicked()", BTN_Cancel, "cancelClicked()", BTN_Help, "displayHelp()");
  29.   }
  30.   return btns
  31. }
  32.  
  33. //***************** LOCAL FUNCTIONS  ******************
  34.  
  35. function initializeUI() {
  36.   var fullPath, fileName, filePath, result;
  37.  
  38.   //get selection and path
  39.   var fullPath = MMNotes.FileInfo_filePath; //grab global, set by launcher
  40.   if (fullPath) {
  41.     readMetafile(fullPath);
  42.     if (FILE_PTR) {
  43.       FILE_IS_WRITEABLE = true;
  44.       if (MMNotes.FileInfo_isWriteable != null && MMNotes.FileInfo_isWriteable == false) FILE_IS_WRITEABLE = false;
  45.       fileName = getFileName(fullPath);
  46.       filePath = getFilePathOnly(fullPath);
  47.       //Initialize the TabControl.  (Pass in the prefix used for the tab layers)
  48.       T = new TabControl('Tab');
  49.       //Add tab pages.   (Pass the layer name, and the page object)
  50.       T.addPage('mainLayer', new Pg1(LABEL_BasicInfo,fileName,filePath));
  51.       T.addPage('allLayer', new Pg2(LABEL_AllInfo,fileName,filePath));
  52.       //Initialize and display the tabs.  (Could pass the name of a page to start on)
  53.       T.start();
  54.     }
  55.   } else {
  56.     cancelClicked();
  57.   }
  58. }
  59.  
  60.  
  61.  
  62. function okClicked() {
  63.   T.finish();
  64.   if (FILE_PTR)
  65.       writeMetafile();
  66.   window.close();
  67. }
  68.  
  69. function cancelClicked() {
  70.   if (FILE_PTR)
  71.       MMNotes.close(FILE_PTR);
  72.   window.close();
  73. }
  74.  
  75. function readMetafile(fullPath) {
  76.   var i, keys, temp;
  77.   FILE_PTR = MMNotes.open(fullPath); //open, or create metafile
  78.   if (FILE_PTR) {
  79.     DATA = new NameValuePair();
  80.     keys = MMNotes.getKeys(FILE_PTR); 
  81.     for (i=0; i<keys.length; i++) {
  82.       temp = MMNotes.get(FILE_PTR,keys[i]);
  83.       DATA.set(keys[i], temp);
  84.   } }
  85. }
  86.  
  87. function writeMetafile() {
  88.   var i, names, keys, dataToWrite=false;
  89.  
  90.   //remove old keys
  91.   keys = MMNotes.getKeys(FILE_PTR); 
  92.   for (i=0; i<keys.length; i++) {
  93.     if (DATA.get(keys[i])==null) MMNotes.remove(FILE_PTR,keys[i]); //if not in local list, remove
  94.   }
  95.   MMNotes.remove(FILE_PTR,KEY_STATUS); //clear out the old KEYS
  96.   MMNotes.remove(FILE_PTR,KEY_NOTES);
  97.   MMNotes.remove(FILE_PTR,KEY_OPEN);
  98.  
  99.   names = DATA.getNames();
  100.   for (i=0; i<names.length; i++)  {  //with each local, non-null key
  101.     MMNotes.set(FILE_PTR,names[i],DATA.get(names[i]));  //set it in the file
  102.   }
  103.  
  104.   MMNotes.close(FILE_PTR);
  105. }
  106.  
  107.  
  108. function getFileName(fullPath) {
  109.   var filePath = MMNotes.localURLToFilePath(fullPath);
  110.   var endPos = filePath.lastIndexOf("\\");
  111.   if (endPos == -1) endPos = filePath.lastIndexOf(":");
  112.   return filePath.substring(endPos+1);
  113. }
  114.  
  115.  
  116. function getFilePathOnly(fullPath) {
  117.   var filePath = MMNotes.localURLToFilePath(fullPath);
  118.   var endPos = filePath.lastIndexOf("\\");
  119.   if (endPos == -1) endPos = filePath.lastIndexOf(":");
  120.   return filePath.substring(0,endPos);
  121. }
  122.  
  123.  
  124. function makeValidKey(theStr) {
  125.   theStr = theStr.replace(/(\")/g,""); //disallow "
  126.   theStr = theStr.replace(/(\')/g,""); //disallow '
  127.   return theStr;
  128. }
  129.  
  130. function getSimpleDate() {
  131.   var today = new Date();
  132.   var dateStr = today.toLocaleString();
  133.   dateStr = dateStr.replace(/\S+\s+(\S+\s+\S+\s+\S+)[^\x00]*/,"$1"); //get 2nd-4th words of date
  134.   return dateStr;
  135. }
  136.  
  137. //*************** Pg1 Class *****************
  138.  
  139. function Pg1(theTabLabel, theFileName, theFilePath) {
  140.   this.tabLabel    = theTabLabel;
  141.   this.fileName    = theFileName;
  142.   this.filePath    = theFilePath;
  143.  
  144.   this.listObj     = new ListControl("statusMenu");
  145.   this.notesObj    = findObject("notesField");
  146.   this.openObj     = findObject("openChbx");
  147. }
  148.  
  149.  
  150. //***** methods *****
  151.  
  152. Pg1.prototype.getTabLabel = Pg1_getTabLabel; //required
  153. Pg1.prototype.canLoad = Pg1_canLoad;
  154. Pg1.prototype.load = Pg1_load;
  155. Pg1.prototype.update = Pg1_update;
  156. Pg1.prototype.unload = Pg1_unload;
  157. Pg1.prototype.drawFilename = Pg1_drawFilename;
  158. Pg1.prototype.drawFilepath = Pg1_drawFilepath;
  159.  
  160.  
  161. function Pg1_getTabLabel() {
  162.   return this.tabLabel;
  163. }
  164.  
  165.  
  166. //Called to check if a page can be loaded
  167. //
  168. function Pg1_canLoad() {
  169.   this.drawFilename();
  170.   this.drawFilepath();
  171.   return true;
  172. }
  173.  
  174.  
  175. //Called when the layer for this page is displayed.
  176. // Use this call to initialize controls.
  177.  
  178. function Pg1_load() {
  179.   var i;
  180.  
  181.   with (this) {
  182.     listObj.setAll(STATUS_ITEMS); //load select menu
  183.  
  184.     //get status
  185.     temp = stripSpaces(DATA.get(KEY_STATUS));
  186.     if (!temp) listObj.setIndex(DEFAULT_STATUS);
  187.     else {
  188.       for (i=0; i<listObj.getLen(); i++) { //look for status in menu
  189.         if (listObj.get(i) == temp) {
  190.           listObj.setIndex(i);
  191.           break;
  192.       } }
  193.       if (i == listObj.getLen()) listObj.append(temp); //if nonexistent, add it
  194.     }
  195.  
  196.     //get notes
  197.     temp = DATA.get(KEY_NOTES);
  198.     notesObj.value = temp || "";
  199.  
  200.     //get open flag
  201.     temp = DATA.get(KEY_OPEN);
  202.     if (temp != null) openObj.checked = (temp.toString().toLowerCase() == "true");
  203.   }
  204. }
  205.  
  206.  
  207. //Called when another page is about to be shown, or finish() is called on
  208. // the tabControl.  Use this call to perform any finishing tasks.
  209.  
  210. function Pg1_unload() {
  211.   return true;
  212. }
  213.  
  214.  
  215. //Called when one of the page controls calls the tabControl update function.
  216. // Use this call to respond to user input.
  217. function Pg1_update(theItemName) {
  218.  
  219.   if (FILE_PTR && FILE_IS_WRITEABLE) with (this) {
  220.       if (theItemName == "insertDate") {
  221.         var theDate = getSimpleDate() + ": \n";
  222.         notesObj.value = theDate + notesObj.value;
  223.         DATA.set(KEY_NOTES,notesObj.value || null); //save notes
  224.  
  225.       } else if (theItemName == "statusMenu") {
  226.         DATA.set(KEY_STATUS,listObj.get() || null); //save status
  227.  
  228.       } else if (theItemName == "notesField") {
  229.         DATA.set(KEY_NOTES,notesObj.value || null); //save notes
  230.  
  231.       } else if (theItemName == "openChbx") {
  232.         DATA.set(KEY_OPEN,openObj.checked || null); //save open flag
  233.     }
  234.  
  235.   } else {
  236.     alert(MSG_ReadOnlyFile);
  237.   }
  238. }
  239.  
  240. function Pg1_drawFilename() {
  241.   var fNameObj = findObject("fileName1");
  242.   if (fNameObj) {
  243.     fNameObj.innerHTML = this.fileName;
  244.   }
  245. }
  246.  
  247.  
  248. function Pg1_drawFilepath() {
  249.   var fPathObj = findObject("filePath1");
  250.   if (fPathObj) {
  251.     fPathObj.innerHTML = this.filePath;
  252.   }
  253. }
  254.  
  255.  
  256.  
  257. //***************** Pg2 Class ******************
  258.  
  259. function Pg2(theTabLabel, theFileName, theFilePath) {
  260.   this.tabLabel    = theTabLabel;
  261.   this.fileName    = theFileName;
  262.   this.filePath    = theFilePath;
  263.  
  264.   this.listObj     = new ListControl("allItems");
  265.   this.nameObj     = findObject("itemName")
  266.   this.valueObj    = findObject("itemValue")
  267.   this.keys        = new Array();
  268. }
  269.  
  270.  
  271. //***** methods *****
  272.  
  273. Pg2.prototype.getTabLabel = Pg2_getTabLabel;
  274. Pg2.prototype.canLoad = Pg2_canLoad;
  275. Pg2.prototype.load = Pg2_load;
  276. Pg2.prototype.update = Pg2_update;
  277. Pg2.prototype.unload = Pg2_unload;
  278. Pg2.prototype.drawList = Pg2_drawList;
  279. Pg2.prototype.drawSelection = Pg2_drawSelection;
  280. Pg2.prototype.drawFilename = Pg2_drawFilename;
  281. Pg2.prototype.drawFilepath = Pg2_drawFilepath;
  282.  
  283.  
  284.  
  285. function Pg2_getTabLabel() {
  286.   return this.tabLabel;
  287. }
  288.  
  289.  
  290.  
  291. //Called to check if a page can be loaded
  292. //
  293. function Pg2_canLoad() {
  294.   this.drawFilename();
  295.   this.drawFilepath();
  296.   return true;
  297. }
  298.  
  299.  
  300.  
  301. //Called when the layer for this page is displayed.
  302. // Use this call to initialize controls.
  303. //
  304. function Pg2_load() {
  305.   with (this) {
  306.     drawList();
  307.     listObj.setIndex(0); //select first item
  308.     drawSelection()
  309.   }
  310. }
  311.  
  312.  
  313.  
  314. //Called when another page is about to be shown, or finish() is called on
  315. // the tabControl.  Use this call to perform any finishing tasks.
  316. function Pg2_unload() {
  317.   return true;
  318. }
  319.  
  320.  
  321.  
  322. //Called when one of the page controls calls the tabControl update function.
  323. // Use this call to respond to user input.
  324. function Pg2_update(theItemName) {
  325.   var i, temp, temp2;
  326.  
  327.   if (theItemName == "allItems") with (this) {
  328.     drawSelection();
  329.  
  330.   } else if (FILE_IS_WRITEABLE) {
  331.  
  332.     if (theItemName == "addItem") with (this) {
  333.       listObj.append();
  334.       valueObj.value = "";
  335.       nameObj.value  = "";
  336.       nameObj.focus();
  337.       nameObj.select();
  338.   
  339.     } else if (theItemName == "delItem") with (this) {
  340.       DATA.del(listObj.getIndex());
  341.       listObj.del();
  342.       drawSelection();
  343.   
  344.     } else if (theItemName == "itemName" || theItemName == "itemValue") with (this) {
  345.       temp = nameObj.value;
  346.  
  347.       if (temp.toLowerCase() == "design notes credits") {
  348.         valueObj.value = unescape("%42%72%6F%75%67%68%74 %74%6F %79%6F%75 %62%79...\n%48%65%69%64%69, %4B%65%6E,"+
  349.                                   " %53%74%65%70%68%61%6E%69%65, %61%6E%64 %4A%61%6B%65!\n%59%65%65-%68%61%77!");
  350.       }
  351.  
  352.       temp2 = (valueObj.value);
  353.       if (temp) { //if valid name
  354.         temp = stripSpaces(makeValidKey(temp));
  355.         if (temp) { //if valid name
  356.           if (listObj.get()) { //if selection is not blank (not new item)
  357.             var oldName = DATA.getName(listObj.getIndex()); //get prior name
  358.             if (temp != oldName) DATA.changeName(oldName,temp); //if name changed, update it
  359.           }
  360.           DATA.set(temp,temp2);
  361.           drawList();
  362.         } else {
  363.           alert(MSG_InvalidName);
  364.     } } }
  365.  
  366.   } else { //file is not writable
  367.     alert(MSG_ReadOnlyFile);
  368.   }
  369. }
  370.  
  371.  
  372. //Called when the layer for this page is displayed.
  373. // Use this call to initialize controls.
  374. //
  375. function Pg2_drawList() {
  376.   var i, allItems;
  377.  
  378.   with (this) {
  379.     listObj.setAll(DATA.getAll());       //clear out the old list
  380.     if (listObj.getLen() > 0) {
  381.       drawSelection();                      //display stuff in fields
  382.     }
  383.   }
  384. }
  385.  
  386.  
  387. function Pg2_drawSelection() {
  388.   var name, value;
  389.  
  390.   with (this) {
  391.     name = DATA.getName(listObj.getIndex()); //get the name for the current selection
  392.     name = name || ""; //make blank if null
  393.     nameObj.value = name;
  394.     value = DATA.get(name);
  395.     value = value || ""; //make blank if null
  396.     valueObj.value = value;
  397.   }
  398. }
  399.  
  400.  
  401. function Pg2_drawFilename() {
  402.   var fNameObj = findObject("fileName2");
  403.   if (fNameObj) {
  404.     fNameObj.innerHTML = this.fileName;
  405.   }
  406. }
  407.  
  408.  
  409. function Pg2_drawFilepath() {
  410.   var fPathObj = findObject("filePath2");
  411.   if (fPathObj) {
  412.     fPathObj.innerHTML = this.filePath;
  413.   }
  414. }
  415.  
  416. //***************** End of Pg2 Class ******************
  417.  
  418.